# Makefile for SmartBASIC 1.x
#
# Assembles SmartBASIC with z80asm and packages it into a bootable
# Coleco ADAM EOS DDP image using eosfs.
#
#   make            assemble + build the bootable .ddp image
#   make bin        assemble only (produces the .bin)
#   make ddp        build the .ddp image (implies bin)
#   make list       show the EOS directory of the built image
#   make clean      remove build artifacts (.bin, .o, .ddp)
#   make distclean  also remove the locally-built eosfs tool
#
# Note: the source/output filenames contain spaces, so the real work is done
# in quoted recipe commands and the targets themselves are phony.  That keeps
# GNU make (which handles spaces in file targets very poorly) out of the way.

# ---- Project files -------------------------------------------------------
NAME    := SmartBASIC 1.x Rev-20_SB1X20Y
ASM     := $(NAME).ASM
BIN     := $(NAME).bin
OBJ     := $(NAME).o
DDP     := $(NAME).ddp

# ---- EOS packaging parameters --------------------------------------------
# NOTE: no trailing inline comments on these assignments -- make would fold
# the whitespace before the '#' into the value.
# name of the file inside the EOS directory
EOSNAME := SMARTBASICH
# DDP volume label (<= 11 chars)
VOLUME  := SB1.x
# attribute byte: system|user|delete-protect|write-protect
ATTR    := 0xD8
DATE    := $(shell date +%Y-%m-%d)

# ---- Tools ---------------------------------------------------------------
Z80ASM  := z80asm

# eosfs: prefer a copy already on PATH; otherwise fetch + build one locally.
EOSFS_REPO := https://github.com/tschak909/eosfs
EOSFS_DIR  := eosfs
EOSFS      := $(shell command -v eosfs 2>/dev/null || echo $(EOSFS_DIR)/eosfs)

.PHONY: all bin ddp list eosfs-tool clean distclean

all: ddp

# ---- Assemble ------------------------------------------------------------
bin:
	$(Z80ASM) -b "$(ASM)"

# ---- Build the bootable DDP image ----------------------------------------
ddp: bin | eosfs-tool
	@rm -f "$(DDP)"
	"$(EOSFS)" create "$(DDP)" ddp256 -v "$(VOLUME)"
	"$(EOSFS)" add    "$(DDP)" "$(BIN)" --name "$(EOSNAME)" --date "$(DATE)"
	"$(EOSFS)" attr   "$(DDP)" "$(EOSNAME)" $(ATTR)
	"$(EOSFS)" boot   "$(DDP)" --file "$(EOSNAME)"
	@echo "Built $(DDP)"

list: | eosfs-tool
	"$(EOSFS)" list "$(DDP)"

# ---- Ensure eosfs is available -------------------------------------------
# Uses eosfs from PATH if present; otherwise clones the latest source from
# GitHub into $(EOSFS_DIR) and builds it.
eosfs-tool:
	@if command -v eosfs >/dev/null 2>&1; then \
	    echo "Using eosfs from PATH: $$(command -v eosfs)"; \
	elif [ -x "$(EOSFS_DIR)/eosfs" ]; then \
	    echo "Using local eosfs: $(EOSFS_DIR)/eosfs"; \
	else \
	    echo "eosfs not found; fetching from $(EOSFS_REPO)"; \
	    if [ -d "$(EOSFS_DIR)/.git" ]; then \
	        git -C "$(EOSFS_DIR)" pull --ff-only; \
	    else \
	        git clone --depth 1 "$(EOSFS_REPO)" "$(EOSFS_DIR)"; \
	    fi; \
	    $(MAKE) -C "$(EOSFS_DIR)"; \
	fi

# ---- Housekeeping --------------------------------------------------------
clean:
	rm -f "$(BIN)" "$(OBJ)" "$(DDP)"

distclean: clean
	rm -rf "$(EOSFS_DIR)"
